﻿using System;
using System.Data;
using System.Linq;
using System.Windows;
using WPFExcelReport.Chart;
using System.Windows.Controls;
using WPFExcelReport.Helpers;
using System.ComponentModel;
using ReflectionStudio.Core.Events;
using System.Threading;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TUBTVKP
    {
        #region ==EXPIND===========
        public static readonly DependencyProperty EXPINDProperty = DependencyProperty.Register("EXPIND",
        typeof(string), typeof(TUBTVKP),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEXPINDChanged));
        public string EXPIND
        {
            get
            {
                return (string)GetValue(EXPINDProperty);
            }
            set
            {
                SetValue(EXPINDProperty, value);
            }
        }
        private static void OnEXPINDChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TUBTVKP;
            if (src != null)
            {
                src.EXPIND = (string)e.NewValue;
            }
        }
        #endregion
        ITXEXRPT ITXEXRPT;
        public TPUTVKP CTXT
        {
            get
            {
                return this.cTxt;
            }
        }
        public TPUTZB CZB
        {
            get
            {
                return this.cZB;
            }
        }
        public TPUTZBQTY CZBQTY
        {
            get
            {
                return this.cZBQTY;
            }
        }
        public TPUTVKPREMARK CTXTR
        {
            get
            {
                return this.cTxtR;
            }
        }
        public TPUTVKPQUALITY CTXTQ 
        {
            get
            {
                return this.cTxtQ;
            }
        }
        public TSSD0114 IPACK
        {
            get
            {
                return this.IP;
            }
        }
        public TSSD0209 OPACK
        {
            get
            {
                return this.OP;
            }
        }
        public TUBTVKP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVKPOS();
            this.DataContext = MV;
            MESSAGES += "VKKNEW;VVRELOAD;VVTCHG;VKPCHK;WDCHG;";
            InitializeComponent();
            VKPSA = "  ";
            ITXEXRPT = new ITXEXRPT();
            ITXEXRPT.Root = "C:\\IntexReport";
            ITXEXRPT.strCompany = app.company;
            ITXEXRPT.PrintType = "OpenExcel";

        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);

            if (dwMaster == null)
            {
                OVERSHORT = "";
                PCS = "";
                TXTVKPSIND = "";
                QTY = 0;

            }
            else
            {
                OVERSHORT = TMV.VKPZUSATZ.Substring(0, 30);
                PCS = TMV.VKPZUSATZ.Substring(30);
                TXTVKPSIND = (TMV.VKPAUFKRS + TMV.VKPAUFLFD + TMV.VKPAUFPOS + "VKT").PadRight(60);
                QTY = TMV.VKPMENGE1;
            }
            if (TMV.VKPART.Trim().Length > 2)
            {
                TXTISIND = (TMV.VKPAUFKRS + TMV.VKPAUFLFD + TMV.VKPKOLL + TMV.VKPART).PadRight(60);
                if(IsCopyTxtMode)
                {
                    if(srcPIND=="")
                        srcPIND = PIND;
                }
            }
            else
            {

                TXTISIND = "";
            }
        }
        public override void OnVAFChg(DataRow _dwVF)
        {
            if (dwMaster == null)
                return;
            base.OnVAFChg(_dwVF);
            VART = TMV.VKPKOLL + TMV.VKPART + TMV.VKPFARBE;
            if (TMV.VKPART.Trim().Length > 5)
            {
                TXTISIND = (TMV.VKPAUFKRS + TMV.VKPAUFLFD + TMV.VKPKOLL + TMV.VKPART).PadRight(60);
                /*
                if (srcPIND != PIND)
                { 
                    if(srcPIND=="")
                    {
                        srcPIND = PIND;
                        return;
                    }
                    TMV.TKON.COPYPIND(srcPIND, PIND); 
                }    */
            }
            else
            {
                TXTISIND = "";
            }
        }
        public override void OnPCSChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.VKPZUSATZ = OVERSHORT.Trim().PadRight(30) + PCS.Trim().PadRight(5);
            base.OnPCSChg();
        }
        public override void OnOVERSHORTChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.VKPZUSATZ = OVERSHORT.Trim().PadRight(30) + PCS.Trim().PadRight(5);
            base.OnOVERSHORTChg();
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            base.NewCommand(sender, e);
        }
        /// <summary>
        /// 因为新建是从这个函数TUBTVKP进入的，所以会先执行这个地方，base.CheckPrice 在这个函数体CheckPrice（）里面执行
        /// </summary>
        /// <param name="dwData"></param>
        public override void CheckPrice(DataRow dwData)
        {
            base.CheckPrice(dwData);
            //这里专门为UBT撰写了一个默认值。注意如果不是标准单位，系数也要跟着更改
            dwData["VKPMEEH"] = "M  ";
        }
        string srcPIND = "";
        private void CopyTXTCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsCopyTxtMode)
                    return; 
                if (dwMaster == null)
                {
                    throw new Exception("请选择一行作为标准");
                }
                cTxt.IsEdit = true;
                /*
                foreach (DataRow row in TBMain.Rows)
                {
                    TXTISIND = (row["VKPAUFKRS"].ToString() + row["VKPAUFLFD"].ToString() + row["VKPKOLL"].ToString() + row["VKPART"].ToString() ).PadRight(60);
                    Thread.Sleep(600);
                    if (srcPIND == PIND) continue;
                    TMV.TKON.COPYPIND(srcPIND, PIND);
                }*/
                if(srcPIND==PIND)
                    throw new Exception("不正确的选择");
                TMV.TKON.COPYPIND(srcPIND, PIND);
                IsCopyTxtMode = false;
                string key = TXTISIND;
                TXTISIND = "";
                TXTISIND = key;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnCopyTxtModeChg(bool copymode)
        {
            srcPIND = "";
            if (!copymode)
                return;           
            MessageBox.Show("请先选择源，然后再点击目标");
            base.OnCopyTxtModeChg(copymode);
        }
        private void CopyEXTXTCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (EXPIND == "")
                    return;
                if (EXPIND == PIND) 
                    return;
                TMV.TKON.COPYPIND(EXPIND, PIND);
                string key = TXTISIND;
                TXTISIND = "";
                TXTISIND = key;

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {

            IsPringPR = true;
        }
        private void btnPrintCN_Click(object sender, RoutedEventArgs e)
        {
            IsPringPRCN = true;
        }
        private void btnPrintEN_Click(object sender, RoutedEventArgs e)
        {
            IsPringPREN = true;
        }

    }
}
